package com.liancheng.smarthome.base;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.databinding.DataBindingUtil;
import android.databinding.ViewDataBinding;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.annotation.StringRes;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

//import com.ijustyce.fastandroiddev3.R;
//import com.ijustyce.fastandroiddev3.http.HttpManager;
//import com.ijustyce.fastandroiddev3.ui.ProgressDialog;
//
import com.liancheng.smarthome.R;
import com.liancheng.smarthome.utils.common.MvpInstantiationUtils;
import com.liancheng.smarthome.utils.logs.LogTag;
import com.liancheng.smarthome.utils.ui.ProgressDialog;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

/**
 * Created by yangchun on 2016/11/12.
 */

public abstract class BaseFragment<T extends BaseFragmentVM,Bind extends ViewDataBinding> extends Fragment {

    public Bind contentView;
    public Context context;
    public Activity activity;
    public View mView;
    public Bundle arguments;
    public T VM;
    private int statusBarHeight;

    @Nullable
    @Override
    public final View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container,
                                   @Nullable Bundle savedInstanceState) {
        if (mView != null) return mView;
        if (cancelCreate()) {
            getActivity().finish();
            return null;
        }
        VM = MvpInstantiationUtils.getInstantiation(this, MvpInstantiationUtils.FIRSTGENERICITY);
        if (VM != null) {
            VM.bindViewAndModel(this);
        }
        contentView = DataBindingUtil.inflate(inflater, getLayoutId(), container, false);
        mView = contentView.getRoot();
//        getStateBar3();
        ViewGroup parent = (ViewGroup) mView.getParent();
        if (parent != null) {
            parent.removeView(mView);
        }
        arguments = getArguments();
        return mView;
    }

    @Override
    public final void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        context = getContext();
        activity = getActivity();
        initViewAndData();
        afterCreate();
    }

    private void getStateBar3(){
        int result = 0;
        int resourceId = this.getResources().getIdentifier("status_bar_height", "dimen", "android");
        if (resourceId > 0) {
            result = this.getResources().getDimensionPixelSize(resourceId);
        }
        LogTag.d("状态栏高度3：" + result + "px");
        statusBarHeight = result;
        View byId = mView.findViewById(R.id.v_title_on);
        try{
            if(byId!=null){
                ViewGroup.LayoutParams params = byId.getLayoutParams();
                params.height=result;
                LogTag.d("setAct:"+this.getClass().getSimpleName()+"-->"+params.height);
                byId.setLayoutParams(params);
            }else {
                LogTag.d("actName:"+this.getClass().getSimpleName());
            }
        }catch (Exception e){
            e.printStackTrace();
            LogTag.d("actName:"+this.getClass().getSimpleName());
        }
    }

    public int getStatusBarHeight() {
        return statusBarHeight;
    }
    @Override
    public void onPause() {
        super.onPause();
        dismissProcess();
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        dismissProcess();
        dialog = null;
        contentView = null;
        context = null;
        activity = null;
    }

    public abstract int getLayoutId();
    public void afterCreate(){};
    protected void initViewAndData(){};

    public boolean cancelCreate() {
        return false;
    }
    public void closeRefresh(){}

    private ProgressDialog dialog;
    public Dialog getDialog() {
        if (dialog == null && activity != null) {
            dialog = new ProgressDialog(activity, getResString(R.string.net_loading_hint));
        }
        return dialog;
    }

    public void showProcess(){
        Dialog dialog = getDialog();
        if (dialog != null) dialog.show();
    }

    public void dismissProcess(){
        if (dialog != null && dialog.isShowing()) dialog.cancel();
    }

    public void backClick(View view) {
        if (activity != null) activity.finish();
    }

    public final String getResString (@StringRes int stringId) {
        try{
            return getResources().getString(stringId);
        }catch (Exception e) {
            e.printStackTrace();
        }
        return "";
    }

    public final int getResColor(int color) {
        return this.getResources().getColor(color);
    }

    public boolean hasNull(){
        return contentView == null;
    }
}